// pages/set/set.js
const app = getApp();
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    domain: app.api.domain,
    date: '1991-01-01',
    userInfo: null,
    code: null,
    phone: ''

  },
  // 获取手机号
  getPhoneNumber(e) {
    const that = this;
    console.log(e,9999)
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      // console.log(e.detail.code)  // 动态令牌
      // console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
      // console.log(e.detail.errno)  // 错误码（失败时返回）

      let token = app.globalData.token;
      let json = {
        token,
        // encryptedData: e.detail.encryptedData,
        // iv: e.detail.iv,
        code: e.detail.code //that.data.code
      }
      let jsonData = app.utils.getSignData(json);
      that.setData({
        hiddenLoading: false
      })
      app.utils.post(app.api.reqUrls.getMiniPhone, jsonData).then(res => {
        console.log(res)
        that.setData({
          hiddenLoading: true
        })
        if (res.status == 1) {
          that.setData({
            'userInfo.mobile': res.result.phoneNumber,
            'userInfo.mobile_validated': 1
          })
          // wx.login({
          //   success: function (res) {
          //     if (res.errMsg == "login:ok") {
          //       that.setData({
          //         code: res.code
          //       })
          //     }
          //   }
          // })
        } else {
          throw res.msg
        }
      }).catch(err => {
        that.setData({
          hiddenLoading: true
        })
        // wx.login({
        //   success: function (res) {
        //     if (res.errMsg == "login:ok") {
        //       that.setData({
        //         code: res.code
        //       })
        //     }
        //   }
        // })
        app.showInfo(err.toString())
      })
    } else {
      return false;
    }

  },
  onChooseAvatar(e) {
    const that = this;
    const {
      avatarUrl
    } = e.detail
    let token = app.globalData.token;
    wx.uploadFile({
      filePath: avatarUrl,
      name: 'head_pic',
      url: app.api.reqUrls.uploadHeadPic,
      method: "post",
      header: {
        "Content-Type": "multipart/form-data"
      },
      success(porp) {
        let head_pic = JSON.parse(porp.data)
        let json = {
          token,
          head_pic: head_pic.result
        }
        let jsonData = app.utils.getSignData(json);
        that.setData({
          hiddenLoading: false
        })
        app.utils.post(app.api.reqUrls.setHeadPic, jsonData).then(res => {
          that.setData({
            hiddenLoading: true
          })
          if (res.status == 1) {
            wx.showToast({
              title: '设置成功',
              icon: 'success',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: '设置失败',
              icon: 'error',
              duration: 2000
            })
          }
        })
      },
      error(err) {
        console.log(err)
      }
    })

    this.setData({
      ['userInfo.head_pic']: avatarUrl
    })
  },
  // 获取生日
  bindDateChange(e) {
    const that = this;
    const token = app.globalData.token;
    that.setData({
      'userInfo.ctime': e.detail.value
    })
    let json = {
      token,
      birthday: e.detail.value
    }
    let jsonData = app.utils.getSignData(json);
    app.utils.post(app.api.reqUrls.sets, jsonData).then(res => {
      if (res.status == 1) {

      }
    }).catch(err => {
      app.showInfo(err.toString())
    })
    this.setData({
      date: e.detail.value
    })
  },
  // 设置密码
  toSetPassword() {
    const that = this;
    if (that.data.userInfo.mobile_validated == 0) {
      wx.showModal({
        content: '请先绑定手机号码',
        showCancel: false,
        success(res) {

        }
      })
    } else {
      wx.navigateTo({
        url: '/page/detail/pages/setPassword/setPassword'
      })
    }

  },
  //设置名称
  bindInput(e) {
    const that = this;
    const token = app.globalData.token;
    if (e.detail.value) {
      let json = {
        token,
        nickname: e.detail.value
      }
      let jsonData = app.utils.getSignData(json);
      that.setData({
        hiddenLoading: false
      })
      app.utils.post(app.api.reqUrls.setNickname, jsonData).then(res => {
        that.setData({
          hiddenLoading: true
        })
        if (res.status == 1) {
          wx.showToast({
            title: '设置成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '设置失败',
            icon: 'error',
            duration: 2000
          })
        }
      })
    } else {
      wx.showToast({
        title: '请输入用户名',
        icon: 'error',
        duration: 2000
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    // wx.login({
    //   success: function (res) {
    //     if (res.errMsg == "login:ok") {
    //       that.setData({
    //         code: res.code
    //       })
    //     }
    //   }
    // })
    const token = app.globalData.token;
    let json = {
      token
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.getSet, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        if (res.result.sex == 0) {
          res.result.sex = ""
        } else if (res.result.sex == 1) {
          res.result.sex = "男"
        } else if (res.result.sex == 2) {
          res.result.sex = "女"
        }
        res.result.head_pic = res.result.head_pic.indexOf('http') >= 0 ? res.result.head_pic : that.data.domain + res.result.head_pic
        // 格式化时间
        res.result.ctime = app.moment(res.result.birthday * 1000).format('YYYY-MM-DD');
        that.setData({
          hiddenLoading: true,
          userInfo: res.result

        })
      } else {
        throw res.msg;
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },

})